package com.response; /**
 * @author wzm
 * @create 2023-04-28 10:39
 */

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;

@WebServlet("/rs3")
public class ResponseServlet3 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //相应一个图片到浏览器

        //准备一个图片 读取F盘上的a.png图片   500000
        FileInputStream fis = new FileInputStream("f://a.png");

        //1.获取字节输出流
        ServletOutputStream is = response.getOutputStream();


        //将文件输入的流复制到输出流
        //定义一个数组存储每次从流中读取的字节数    11231534564564567987157987
        byte[] buff = new byte[1024];

        //循环将流中所有的字节数据读取出来
        int read = 0 ;
        //每次读取1024字节放入到数组中 如果读取没有数据了 返回-1
        while((read =fis.read(buff)) != -1){
            //读一次向浏览器写一次
            //2.通过字节输出流对象向浏览器输出字节数据（二进制数据）
            is.write(buff,0,read);
        }

        //3.关闭创建的出入流   response的流 自己会关闭
        fis.close();







    }
}
